//========================================================================//
//      Copyright 2012, Smilodon Studio Inc.
//      All rights reserved.
//========================================================================//

/**
 *	base
 *      @author: CYS
 */


var canvas;
var g2D;
var g_infoShow;

var timer = {time: 0, dtime: 0, startime: Date.now(), lasttime: Date.now() };
var mouse = {x: 0, y: 0, x0: 0, y0: 0, dx: 0, dy: 0, rx: 0, ry: 0, down: false, left: false, right: false};


var height;
var width;
var fullWindow = true;
var ratio = 1;

var go;



function canvasResize()
{

	ratio = window.innerWidth/canvas.width;
	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;
	width = canvas.width;
	height = canvas.height;
	if(!g2D) g2D = canvas.getContext("2d");
	if(go) go.resize(width, height, g2D);

}



function init()
{

	canvas = document.getElementById('canvas3D');

	canvasResize();

	go = new Go();
	go.resize(width, height, g2D);


	var touchable = 'createTouch' in document;
	if (touchable)
	{
		canvas.addEventListener('touchstart', onTouchStart, false);
		canvas.addEventListener('touchmove', onTouchMove, false);
		canvas.addEventListener('touchend', onTouchEnd, false);
	}
	else
	{
		canvas.addEventListener('mousedown', onTouchStart, false);
		canvas.addEventListener('mousemove', onTouchMove, false);
		canvas.addEventListener('mouseup', onTouchEnd, false);

	}

	canvas.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
	document.addEventListener('keypress', onKeyPress, false);
	window.addEventListener('keydown', onKeyDown, false);
	window.addEventListener('keyup', onKeyUp, false);

	g2D.lineWidth = 1;
	g_infoShow = new infoShow();

}



var bTimeout = false;
window.onresize = function()
{
	if (fullWindow) canvasResize();
	bTimeout = true;
}

var first = true;
var lastOrien;
window.ondeviceorientation =  function(e)
{
	if(first)
	{
		lastOrien = window.orientation;
		first = false;
	}

	if(window.orientation !== lastOrien)
	{
		window.onresize();
		lastOrien = window.orientation;
	}

	//g_infoShow.add("alpa: ", e.alpha);
	//g_infoShow.add("beta: ",e.beta);
	//g_infoShow.add("gamma: ",e.gamma);
	//g_infoShow.add("orientation: ",window.orientation);
}

window.onblur = function()
{
	bTimeout = true;
}




function drawScene()
{
	// clear screen
	//g2D.fillStyle = "#000000";
	//g2D.fillRect(0,0,width,height);
	go.draw(g2D);

	// g_infoShow.show();

}



function onKeyPress(event)
{
	switch(event.keyCode || event.which)
	{
		case 13: // Enter
			event.preventDefault();
			break;

	}


}

function onKeyDown(event)
{
	// g_infoShow.add("event.keyCode:  ", event.keyCode);
	// g_infoShow.add("event.which:  ", event.which);

	switch(event.keyCode || event.which)
	{
		case 38: // Down Arrow
			event.preventDefault();
			break;

		case 39: // UP Arrow
			event.preventDefault();
			break;

		case 40: // Down Arrow
			event.preventDefault();
			break;
	}

}


function onKeyUp(event)
{
	switch(event.keyCode || event.which)
	{

		case 39: //
			event.preventDefault();
			break;

		case 40: // Down Arrow
			event.preventDefault();
			break;
	}
}


function onTouchStart(event)
{
	// Prevent the browser from doing its default thing (scroll, zoom)
	event.preventDefault();


	if(event.clientX) // on mouse down
	{
		mouse.x = mouse.x0 = event.clientX;
		mouse.y = mouse.y0 = event.clientY;
		mouse.dx = mouse.dy = mouse.rx = mouse.ry = 0;
		// g_infoShow.add("mouse.rx:  ", mouse.rx);
		// g_infoShow.add("mouse.ry:  ", mouse.ry);

		mouse.down = true;

	}

	if(!event.touches) return;

	// on touch star

	for(var i = 0; i < event.touches.length; i++)
	{
		mouse.x = mouse.x0 = event.touches[0].clientX;
		mouse.y = mouse.y0 = event.touches[0].clientY;
		mouse.dx = mouse.dy = mouse.rx = mouse.ry = 0;
		mouse.down = true;

	}

}

function onTouchMove(event)
{
	event.preventDefault();

	if(event.clientX && mouse.down) // on mouse move
	{
		mouse.dx = event.clientX - mouse.x0;
		mouse.dy = event.clientY - mouse.y0;

		mouse.rx += mouse.dx;
		mouse.ry += mouse.dy;
		// g_infoShow.add("mouse.rx:  ", mouse.rx);
		// g_infoShow.add("mouse.ry:  ", mouse.ry);

		mouse.x0 = mouse.x = event.clientX;
		mouse.y0 = mouse.y = event.clientY;
	}

	if(!event.touches) return;

	for(var i = 0; i < event.touches.length; i++)
	{
		var x = event.touches[0].clientX;
		var y = event.touches[0].clientY;

		mouse.dx = x - mouse.x0;
		mouse.dy = y - mouse.y0;
		mouse.rx += mouse.dx;
		mouse.ry += mouse.dy;

		mouse.x0 = mouse.x = x;
		mouse.y0 = mouse.y = y;

	}


}

function onTouchEnd(event)
{
	event.preventDefault();

	if(event.clientX) // on mouse up
	{

		mouse.x = event.clientX;
		mouse.y = event.clientY;

		if(event.button === 2 || event.button === 3)mouse.right = true;
		else mouse.left = true;

		//if(Math.abs(mouse.rx) < 10 && Math.abs(mouse.ry) < 10) go.onClick(mouse.x, mouse.y);
		go.onClick(mouse.x, mouse.y);

		mouse.down = false;

	}

	if(!event.touches) return;

	mouse.dx = 0;
	mouse.dy = 0;
	//mouse.x = event.touches[0].clientX;
	//mouse.y = event.touches[0].clientY;
	go.onClick(mouse.x, mouse.y);

	mouse.down = false;
}



function gameloop()
{
	window.requestAnimFrame( gameloop );
	if(bTimeout)
	{
		timer.lasttime = Date.now();
		bTimeout = false;
	}
	timer.dtime = Date.now() - timer.lasttime;
	timer.time = Date.now() - timer.startime;
	drawScene();
	timer.lasttime = Date.now();

}


function game()
{
	init();
	gameloop();
}




